#!/bin/sh
# Controls color mode of the image

MODE_FILE="/tmp/colormode.txt"
[ -f "$MODE_FILE" ] || touch "$MODE_FILE"

# NB! ISP mode is set backwards: 0 - color, 1 - monochrome

switch_to_color() {
	prudyntctl json '{"image":{"running_mode":0}}'
	echo "1" > "$MODE_FILE"
}

switch_to_monochrome() {
	prudyntctl json '{"image":{"running_mode":1}}'
	echo "0" > "$MODE_FILE"
}

case "$1" in
	0 | off | night)
		switch_to_monochrome
		;;
	1 | on | day)
		switch_to_color
		;;
	~ | toggle)
		if [ "$(cat "$MODE_FILE")" -eq 1 ]; then
			switch_to_monochrome
		else
			switch_to_color
		fi
		;;
	status)
		cat "$MODE_FILE"
		;;
	\? | read)
		cat "$MODE_FILE" | tr -d '\n'
		;;
	*)
		echo "Usage: $0 [on|off|toggle|status|read]" >&2
		exit 1
		;;
esac

exit 0
